# -*- coding:utf-8 -*-

import time
import xlwt
from utils.GetWorkspacePath import GetworkspacePath
from os import path

class WriteToExcel():
    def openExcel(self):
        self.workbook = xlwt.Workbook(encoding='utf-8')
        self.sheet = self.workbook.add_sheet('detail')
        self.__writeTableTitle()
        self.author = None

    def __writeTableTitle(self):
        self.sheet.write(0, 0, label="Seq")
        self.sheet.write(0, 1, label="Title")
        self.sheet.write(0, 2, label="Author")
        self.sheet.write(0, 3, label="Date")
        self.sheet.write(0, 4, label="Duration")
        self.sheet.write(0, 5, label="Count")

    def writeData(self, i, value):
        if not self.author:
            self.author = value[3]
        self.sheet.write(i + 1, 0, label=str(i+1))
        self.sheet.write(i + 1, 1, label=value[4])
        self.sheet.write(i + 1, 2, label=value[3])
        self.sheet.write(i + 1, 3, label=value[2])
        self.sheet.write(i + 1, 4, label=value[1])
        self.sheet.write(i + 1, 5, label=value[0])

    def saveExcel(self):
        self.workbook.save(path.join(GetworkspacePath().WORKSPACE_PATH, "output", self.author + "_" + time.strftime('%Y%m%d_%H%M%S',time.localtime(time.time())) +r'.xls'))


if __name__ == '__main__':
    l = [
        ['197,312次观看', '4分钟29秒钟', '8个月前', '来自回形针PaperClip', '【提前动手】Vol.002 我们做了个公开成本的 T 恤套装'],
        ['77,532次观看', '7分钟29秒钟', '8个月前', '来自回形针PaperClip', 'Vol.177 地震预报为什么这么难？'],
        ['60,474次观看', '4分钟25秒钟', '8个月前', '来自回形针PaperClip', '【提前动手】Vol.001 我们做了三面镜子：挺钢的，有点硬']
    ]

    we = WriteToExcel()
    we.openExcel()
    for i, a in enumerate(l):
        we.writeData(i, a)

    we.saveExcel()
